package com.itheima.app.controller;

import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    @PostMapping("/user/save")
    public Long save(@RequestBody User user) {
        return userManager.save(user);
    }

    @GetMapping("/user/findByMobile")
    public User findByMobile(String mobile) {
        return userManager.findByMobile(mobile);
    }

    //发送手机验证码
    @PostMapping("/user/login")
    public void sendCode(@RequestBody Map<String, String> map) {
        //获取手机号,调用manager完成发送验证码操作
        String phone = map.get("phone");
        userManager.sendCode(phone);
    }

    //注册登陆
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String, String> map) {
        return userManager.regAndLogin(map);
    }

    //完善用户基本资料
    @PostMapping("/user/loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        return userManager.saveUserInfo(userInfo, token);
    }

    //完善用户头像
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public ResponseEntity saveUserInfoHead(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        return userManager.saveUserInfoHead(headPhoto, token);
    }

    //获取用户基本信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoById(@RequestParam(required = false) Long userID, @RequestHeader("Authorization") String token) {
        //1.判断userID有无值
        //2.若有值,调用manager查询数据
        if (userID != null) {
            return userManager.findUserInfoById(userID);
        } else {
            //3.若无值,从token中获取用户信息,调用manager查询数据
            User user = userManager.getUserFromToken(token);
            if (user == null) {
                return ResponseEntity.status(401).body(null);
            }
            return userManager.findUserInfoById(user.getId());
        }
    }

    //更新用户信息
    @PutMapping("/users")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        return userManager.updateUserInfo(userInfo, token);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     */
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendVerificationCode() {
        userManager.sendVerificationCode();

    }

    /**
     * 修改手机号 - 2 校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> map) {
        //获取前端传来的验证码
        String code = map.get("verificationCode");
        return userManager.checkVerificationCode(code);

    }

    /**
     * 修改手机号 - 3 保存
     *
     * @return
     */
    @PostMapping("/users/phone")
    public ResponseEntity updatePhone(@RequestBody Map<String, String> map) {
        //获取修改后的手机号
        String phone = map.get("phone");
        return userManager.updatePhone(phone);

    }

}
